// 判断两个值是否相等
export function isEqual(obj1, obj2) {
    // 基本类型比较
    if (obj1 === obj2) {
        return true;
    }
    if (
        typeof obj1 !== 'object' ||
        obj1 === null ||
        typeof obj2 !== 'object' ||
        obj2 === null
    ) {
        return false;
    }

    // 对象和数组比较
    const keys1 = Object.keys(obj1);
    const keys2 = Object.keys(obj2);

    if (keys1.length !== keys2.length) {
        return false;
    }

    for (let key of keys1) {
        const val1 = obj1[key];
        const val2 = obj2[key];
        // 需要递归比较
        if (isEqual(val1, val2)) {
            continue;
        } else {
            return false;
        }
    }

    return true;
}

// 获取差异字段
export const getDiffField = (newObj, oldObj) => {
    const res = [];
    if (!newObj && oldObj) return Object.keys(oldObj);
    if (newObj && !oldObj) return Object.keys(newObj);
    for (let key in newObj) {
        const newVal = newObj[key];
        const oldVal = oldObj[key];
        if (!isEqual(newVal, oldVal)) {
            res.push(key);
        }
    }
    return res;
};
